package com.situ.company.project.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.situ.company.project.dao.IProjectDao;
import com.situ.company.project.model.ProjectModel;
import com.situ.company.util.FmtEmpty;
import com.situ.company.util.JDBCUtil;

public class ProjectDaoImpl implements IProjectDao{
	private static String table = "project";
	private static String cols = "code,name,time";

	@Override
	public Integer insert(ProjectModel model) {
		StringBuffer sql=new StringBuffer("insert into ");
		sql.append(table).append("(").append(cols).append(")values(?,?,?)");
		return JDBCUtil.update(sql.toString(), model.getCode(),model.getName(),model.getTime());
	}

	@Override
	public Integer deleteByCode(ProjectModel model) {
		StringBuffer sql=new StringBuffer("delete from ").append(table).append(" where code=?");
		return JDBCUtil.update(sql.toString(), model.getCode());
		
		
	}

	@Override
	public Integer delete(ProjectModel model) {
		StringBuffer sql=new StringBuffer("delete from ").append(table);
		List<Object> values=appendWhere(sql,model);
		return JDBCUtil.update(sql.toString(), values);
	}

	@Override
	public Integer updateAll(ProjectModel model) {
		StringBuffer sql=new StringBuffer("update ");
		sql.append(table).append(" set name=?,time=? where code=?");
		return JDBCUtil.update(sql.toString(), model.getName(),model.getTime(),model.getCode());
	}

	@Override
	public Integer updateActive(ProjectModel model) {
		StringBuffer sql=new StringBuffer("update ");
		sql.append(table).append(" set id=id");
		List<Object> values=appendSet(sql,model);
		return JDBCUtil.update(sql.toString(), values);
	}


	@Override
	public List<ProjectModel> selectList(ProjectModel model) {
		StringBuffer sql=getSqlSel();
		List<Object> values=appendWhere(sql, model);
		Map<String,String> fields=getFields();
		return JDBCUtil.queryList(sql.toString(), values, ProjectModel.class,fields);
	}

	@Override
	public ProjectModel selectModel(ProjectModel model) {
		StringBuffer sql=getSqlSel().append(" where code=? ");
		List<Object> values = Arrays.asList(model.getCode());
		Map<String,String> fields=getFields();
		return JDBCUtil.queryModel(sql.toString(), values, ProjectModel.class,fields);
	}
	private StringBuffer getSqlSel() {
		StringBuffer sql=new StringBuffer(" select id,code,name,time ");
		sql.append(" from ").append(table);
		return sql;
	}
	
	private Map<String, String> getFields() {
		Map<String,String> fields=new HashMap<>();
		fields.put("id", "id");
		fields.put("code", "code");
		fields.put("name", "name");
		fields.put("time", "time");
		return fields;
	}

	@Override
	public Integer selectCount(ProjectModel model) {
		StringBuffer sql=new StringBuffer(" select count(1) from project ");
		List<Object> vals=appendWhere(sql, model);
		
		return JDBCUtil.queryInt(sql.toString(), vals);
	}
	
	private List<Object> appendWhere(StringBuffer sql, ProjectModel model) {
		sql.append(" where 1=1 ");
		List<Object> values=new ArrayList<Object>();
		String code =model.getCode();
		if(!FmtEmpty.isEmpty(code)) {
			sql.append(" and code like ?");
			values.add(code);
		}
		String name =model.getName();
		if(!FmtEmpty.isEmpty(name)) {
			sql.append(" and name like ?");
			values.add(name);
		}
		String time =model.getTime();
		if(!FmtEmpty.isEmpty(time)) {
			sql.append(" and time like ?");
			values.add(time);
		}
		if(model.isPageOn()) {
			sql.append(" limit ?,?");
			values.add(model.getRowStart());
			values.add(model.getRowCount());
		}
		
		return values;
	}
	
	private List<Object> appendSet(StringBuffer sql, ProjectModel model) {
		List<Object> values=new ArrayList<Object>();
		String name=model.getName();
		if(name!=null) {
			sql.append(", name=? ");
			values.add(name);
		}
		String time=model.getTime();
		if(time!=null) {
			sql.append(", time=? ");
			values.add(time);
		}
		sql.append(" where code=?");
		values.add(model.getCode());
		return values;
	}

}
